﻿//  Copyright (c) 2009 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html
using System;
using System.Web.Security;
using System.Collections.Generic;
using System.Linq;

namespace DNA.Mvc.Security
{
    public class AccountMembershipService : IMembershipService
    {
        //private MembershipProvider _provider;

        [Microsoft.Practices.Unity.InjectionConstructor]
        public AccountMembershipService()  { }

        //public AccountMembershipService(MembershipProvider provider)
        //{
        //    _provider = provider ?? Membership.Provider;
        //}

        public int MinPasswordLength
        {
            get
            {
                return Membership.MinRequiredPasswordLength;
            }
        }

        public bool ValidateUser(string userName, string password)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("The value could not be null", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("The value could not be null", "password");
            return Membership.ValidateUser(userName, password);
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("The value could not be null", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("The value could not be null", "password");
            if (String.IsNullOrEmpty(email)) throw new ArgumentException("The value could not be null", "email");
            Membership.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("The value could not be null", "userName");
            if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("The value could not be null", "oldPassword");
            if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("The value could not be null", "newPassword");

            try
            {
                MembershipUser currentUser = Membership.GetUser(userName, true /* userIsOnline */);
                return currentUser.ChangePassword(oldPassword, newPassword);
            }
            catch (ArgumentException)
            {
                return false;
            }
            catch (MembershipPasswordException)
            {
                return false;
            }
        }

        public string[] GetSuggestUserNames(string term, int maxResults)
        {
            var total = 0;
            var match = term;
            if (!term.EndsWith("%"))
                match += "%";
            var users = Membership.FindUsersByName(match, 0, maxResults, out total);
            if (users != null)
            {
                var result = new List<string>();
                foreach (MembershipUser user in users)
                    result.Add(user.UserName);
                return result.ToArray();
            }
            return new string[0];
        }
    }

}
